﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using HCDV.DB;
using MySql.Data.MySqlClient;
namespace HCDV.HCSP
{
    /// <summary>
    /// 对外统一数据操作类
    /// </summary>
    public class dbsimple : IDisposable
    {
        public void Dispose()
        {
            GC.SuppressFinalize(true);
        }
        private static string dbtype = BaseConfig.Dbtype;
        public dbsimple()
        { }
        public dbsimple(string cstring)
        { BaseConfig.ConnectionString = cstring; }
        public Idb getdb()
        {
            
            switch (dbtype)
            { 
                case "mysql": return new DbMySql();
                case "sql": return new DbSql();
                default: return null;


            }
        }
        /// <summary>
        /// 获取主键
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public long GetSequence(string fieldName)
        {
            return getdb().GetSequence(fieldName);
        }
        /// <summary>
        /// 执行指定的prc
        /// </summary>
        /// <param name="procname"></param>
        /// <param name="allinfo"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool doProc(string procname, Hashtable allinfo, out string info)
        {
            return getdb().doProc(procname, allinfo, out info);
        }
        public DataSet getProc(string procname, Hashtable allinfo)
        {
            return getdb().getProc(procname, allinfo);
        }
        /// <summary>
        /// 执行SQL
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int exe(string sql)
        {
            return getdb().spExe(sql);
        }
        public bool SpExe(string sql)
        {
            if (getdb().spExe(sql) > 0)
                return true;
            else
                return false;
        }
        public double SpExeBackid(string sql)
        {
            return getdb().spExeBackid(sql);
        }
        /// <summary>
        /// 查询一个字段的值
        /// </summary>
        /// <param name="SQLString"></param>
        /// <returns></returns>
        public string  QueryFV(string SQLString)
        {
            var dt= getdb().query(SQLString);
            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0][0].ToString();
            }
            else return "";
        }
        /// <summary>
        /// 查询SQL
        /// </summary>
        /// <param name="SQLString"></param>
        /// <returns></returns>
        public DataTable Query(string SQLString)
        {
            return getdb().query(SQLString);
        }
        /// <summary>
        /// 查询SQL
        /// </summary>
        /// <param name="SQLString"></param>
        /// <returns></returns>
        public DataSet Queryds(string SQLString)
        {
            return getdb().queryds(SQLString);
        }
        public DataTable getReportPage(string tableInfo, string reFiles, int pagenew, int pagesize, string whereinfo, string orders, ref int allpage)
        {
            DataTable table;

            table = getPageInfo(tableInfo, reFiles, ((pagenew - 1) * pagesize + 1).ToString(), (pagenew * pagesize).ToString(), whereinfo, ref allpage, orders);

            return table;
        }
        public DataTable getReportNoPage(string tableInfo, string reFiles, string whereinfo)
        {
            DataTable table;
            table = getInfo(tableInfo, reFiles, whereinfo);
            return table;
        }
        public DataTable getReportNoPage(string tableInfo, string reFiles, string whereinfo, string orders)
        {
            DataTable table;
            table = getInfo(tableInfo, reFiles, whereinfo, orders);
            return table;
        }

        /// <summary>
        /// 通用查询-分页
        /// </summary>
        /// <param name="tableinfo"></param>
        /// <param name="returnfiles"></param>
        /// <param name="startp"></param>
        /// <param name="endp"></param>
        /// <param name="where"></param>
        /// <param name="pall"></param>
        /// <param name="orders"></param>
        /// <returns></returns>
        private DataTable getPageInfo(string tableinfo, string returnfiles, string startp, string endp, string where, ref int pall, string orders = null)
        {
            return getdb().getPageInfo(tableinfo, returnfiles, startp, endp, where, ref  pall, orders);
        }
        /// <summary>
        /// 通用查询-不分页
        /// </summary>
        /// <param name="tableinfo"></param>
        /// <param name="returnfiles"></param>
        /// <param name="orders"></param>
        /// <returns></returns>
        private DataTable getInfo(string tableinfo, string returnfiles, string where, string orders = null)
        {
            return getdb().getInfo(tableinfo, returnfiles, where, orders);
        }




        #region 工作流查询
        private DataTable getInfohf(LoginUser l, string ywcode, string tableinfo, string returnfiles, string where, string orders = null)
        {
            return getdb().getInfohf(l, ywcode, tableinfo, returnfiles, where, orders);
        }
        public DataTable getReportPagehf( LoginUser l ,string ywcode,string tableInfo, string reFiles, int pagenew, int pagesize, string whereinfo, string orders, out int allpage)
        {
            DataTable table;

            table = getPageInfohf(l, ywcode, tableInfo, reFiles, ((pagenew - 1) * pagesize + 1).ToString(), (pagenew * pagesize).ToString(), whereinfo, out allpage, orders);

            return table;
        }
        public DataTable getReportNoPagehf(LoginUser l, string ywcode, string tableInfo, string reFiles, string whereinfo)
        {
            DataTable table;
            table = getInfohf(l, ywcode, tableInfo, reFiles, whereinfo);
            return table;
        }
        public DataTable getReportNoPagehf(LoginUser l, string ywcode, string tableInfo, string reFiles, string whereinfo, string orders)
        {
            DataTable table;
            table = getInfohf(l, ywcode, tableInfo, reFiles, whereinfo, orders);
            return table;
        }
        private DataTable getPageInfohf(LoginUser l, string ywcode, string tableinfo, string returnfiles, string startp, string endp, string where, out int pall, string orders = null)
        {
            return getdb().getPageInfohf(l,ywcode,tableinfo, returnfiles, startp, endp, where, out  pall, orders);
        }
        #endregion
        #region 非通用性
        /// <summary>
        /// 获取用户菜单
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public DataTable hcGetMenu(string userid)
        {
            if (dbtype == "mysql")
            {
                return Query(string.Format(@"SELECT  c_menuid
      ,c_menuname
      ,c_url
      ,c_display,fshowimg
  FROM  view_menu  where view_menu.id  in(
SELECT 
      AccessValue
  FROM sysObjAccess  where AccessType='menu' and ObjType='user' and ObjValue={0}
  union  
  SELECT
      AccessValue
  FROM sysObjAccess  where AccessType='menu' and ObjType='role' and ObjValue in(
  SELECT  RoleID FROM sysEmpRoles where EmpID={0})
) order by c_menuid", userid));
            }
            else
            {
                return Query(@"SELECT  c_menuid
      ,c_menuname
      ,c_url
      ,c_display,fshowimg
  FROM  view_menu  where view_menu.id  in (select value from GetUserMAccess('" + userid + "')) order by c_menuid ");
            }
        }
        /// <summary>
        /// 获取用户页面按钮
        /// </summary>
        /// <param name="pagecode"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public DataTable hcGetBut(string pagecode, string userid)
        {
            if (dbtype == "mysql")
            {
                return Query(@"SELECT  BtnID as id
      ,BtnIDHtml as code
      ,BtnText as name
      ,BtnIcon as icon
      ,BtnNo as sort 
  FROM sysButton as T inner join sysTab as T1 on T.TabMenuId=T1.pkid and T1.description='" + pagecode + @"' and T.BtnID in (SELECT 
      AccessValue
  FROM sysObjAccess  where AccessType='button' and ObjType='user' and ObjValue=" + userid + @" 
  union 
  SELECT
      AccessValue

  FROM sysObjAccess  where AccessType='button' and ObjType='role' and ObjValue in(
  SELECT  RoleID FROM sysEmpRoles where EmpID=" + userid + ")) and T.BtnIcon != 'notoolbaraccess' order by T.BtnNo");

            }
            else
            {
                return Query(@"SELECT  [BtnID] as id
      ,[BtnIDHtml] as code
      ,[BtnText] as name
      ,[BtnIcon] as icon
      ,[BtnNo] as sort 
  FROM sysButton as T inner join dbo.sysTab as T1 on T.TabMenuId=T1.pkid and T1.description='" + pagecode + "' and T.BtnID in (select * from GetUserBtAccess('" + userid + "')) and T.BtnIcon != 'notoolbaraccess' order by T.BtnNo");

            }
        }
        
        /// <summary>
        /// 插入图片
        /// </summary>
        /// <param name="fullname"></param>
        /// <param name="photos"></param>
        /// <returns></returns>
        public int hcIsysfileinfo(string fullname, byte[] photos, string fimgname, string fpostid)
        {
            using (MySqlConnection conn = new MySqlConnection(HCDV.BaseConfig.ConnectionString))
            {
                string sql = "INSERT INTO sysfileinfo(fieldid,fieldinfo,fimgname,fpostid)VALUES(@id,@fieldinfo,@fimgname,@fpostid) ";
                using (MySqlCommand cmd = new MySqlCommand(sql))
                {
                    MySqlParameter param1 = new MySqlParameter("@id", MySqlDbType.VarChar);
                    param1.Value = fullname;
                    cmd.Parameters.Add(param1);

                    MySqlParameter param = new MySqlParameter("@fieldinfo", MySqlDbType.Binary);
                    param.Value = photos;
                    cmd.Parameters.Add(param);

                    MySqlParameter param2 = new MySqlParameter("@fimgname", MySqlDbType.VarChar);
                    param2.Value = fimgname;
                    cmd.Parameters.Add(param2);


                    MySqlParameter param3 = new MySqlParameter("@fpostid", MySqlDbType.VarChar);
                    param3.Value = fpostid;
                    cmd.Parameters.Add(param3);

                    cmd.Connection = conn;
                    conn.Open();
                    int i = cmd.ExecuteNonQuery();
                    return i;
                }
            }
        }
    
        #endregion


        
    }
}
